package com.qn.controller;

import com.qn.entity.Industry;
import com.qn.entity.location.Provinces;
import com.qn.service.FilterInfoService;
import com.qn.utils.DataAcquisitionWithQiXinBao;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h1>过滤器信息</h1>
 *
 * @author Olo
 */
@RestController
@RequestMapping("/filterInfo")
public class QiXinBaoController {
    @Resource
    FilterInfoService filterInfoService;

    /**
     * 获得行业选项信息
     */
    @GetMapping("/getIndustry")
    public List<Industry> getIndustry() {
        List<Industry> industries = filterInfoService.getIndustryInfo();
        //如果获取过后为空,则从网路上爬取插入到数据库中,再返回
        if (industries.size() == 0) {
            filterInfoService.insertIndustryInfo(DataAcquisitionWithQiXinBao.getIndustry());
            industries = filterInfoService.getIndustryInfo();
        }
        return industries;
    }

    /**
     * 手动添加行业信息(格式->主行业:子行业,子行业...;主行业:子行业,...)
     *
     * @param industries 行业数组
     * @return java.util.List<com.qn.entity.Industry>
     * @description TODO
     * @author olo
     * @date 2021/10/19
     */
    @PostMapping("/manualInsertIndustry")
    public List<Industry> manualInsertIndustry(@RequestBody List<Industry> industries) {
        filterInfoService.insertIndustryInfo(industries);
        return industries;
    }

    /**
     * 获得地区参数
     *
     * @return java.util.List<com.qn.entity.location.Provinces>
     * @description TODO
     * @author 85
     * @date 2021/10/19
     */
    @GetMapping("/getLocation")
    public List<Provinces> getLocation() {
        List<Provinces> provinces = filterInfoService.getLocations();
        if (provinces.size() == 0) {
            filterInfoService.insertLocations(DataAcquisitionWithQiXinBao.getLocations());
            provinces = filterInfoService.getLocations();
        }
        return provinces;
    }
}
